#### II Replication ####
### purpose: replicating figure 4 ####
# installing packages ----

list.of.packages = c('tidyverse', 'ggplot2', 'gridExtra')
new.packages <- list.of.packages[!(list.of.packages %in% 
                                     installed.packages()[,"Package"])]

if(length(new.packages)) install.packages(new.packages)

# loading packages ----

suppressPackageStartupMessages(
  {
    library(tidyverse)
    library(ggplot2)
    library(gridExtra)
  }
)

# loading in data ----
map <- readRDS("~/data/fig_data_map.RDS")

# Figure 4 ----
n <- 111
f4_111 <- ggplot(map |> filter(cong == n), aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=lnamount))+
  coord_fixed() + xlim(-125, -66) +ylim(23, 50) +
  theme_minimal() +
  scale_fill_gradient(low = "#132B43", 
                         high = "#56B1F7", 
                         limits=c(0, 24)) +
  ylab("") + xlab("") +
  ggtitle(paste(n, "th Congress")) +
  theme(legend.position = "none")

n <- 112
f4_112 <- ggplot(map |> filter(cong == n), aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=lnamount))+
  coord_fixed() + xlim(-125, -66) +ylim(23, 50) +
  theme_minimal() +
  scale_fill_gradient(low = "#132B43", 
                      high = "#56B1F7", 
                      limits=c(0, 24)) +
  ylab("") + xlab("") +
  ggtitle(paste(n, "th Congress")) +
  theme(legend.position = "none")

n <- 113
f4_113 <- ggplot(map |> filter(cong == n), aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=lnamount))+
  coord_fixed() + xlim(-125, -66) +ylim(23, 50) +
  theme_minimal() +
  scale_fill_gradient(low = "#132B43", 
                      high = "#56B1F7", 
                      limits=c(0, 24)) +
  ylab("") + xlab("") +
  ggtitle(paste(n, "th Congress")) +
  theme(legend.position = "none")

n <- 114
f4_114 <- ggplot(map |> filter(cong == n), aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=lnamount))+
  coord_fixed() + xlim(-125, -66) +ylim(23, 50) +
  theme_minimal() +
  scale_fill_gradient(low = "#132B43", 
                      high = "#56B1F7", 
                      limits=c(0, 24)) +
  ylab("") + xlab("") +
  ggtitle(paste(n, "th Congress")) +
  theme(legend.position = "none")

n <- 115
f4_115 <- ggplot(map |> filter(cong == n), aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=lnamount))+
  coord_fixed() + xlim(-125, -66) +ylim(23, 50) +
  theme_minimal() +
  scale_fill_gradient(low = "#132B43", 
                      high = "#56B1F7", 
                      limits=c(0, 24)) +
  ylab("") + xlab("") +
  ggtitle(paste(n, "th Congress")) +
  labs(fill = "ln(aid award)")

f4 <- grid.arrange(arrangeGrob(f4_111, f4_112, f4_113, f4_114, nrow=2), f4_115, ncol = 1)

layout_matrix <- matrix(c(1, 1, 1, 2, 2, 2, 
                          3, 3, 3, 4, 4, 4, 
                          6, 5, 5, 5, 5, 6),
                        nrow = 3, byrow = TRUE)

f4 <- grid.arrange(f4_111, f4_112, 
                   f4_113, f4_114, 
                   f4_115, 
                   layout_matrix = layout_matrix)


ggsave(plot = f4, 
       filename = "~/figures/figure4_map.jpeg",
       width = 10, height = 10, dpi = 300)
